package cn.michael.dev.leetcode;

import cn.michael.dev.entity.ListNode;

/**
 * Created by hufenggang on 2021/8/20.
 *
 * 问题描述：给你一个链表的头节点 head ，旋转链表，将链表每个节点向右移动 k 个位置。
 */
public class Problem61 {

    public ListNode rotateRight(ListNode head, int k) {

        while (k > 0) {

            if (head == null || head.next == null) {
                return head;
            }

            ListNode currentNode = head;
            ListNode lastNode = head.next;

            while(lastNode.next != null) {
                currentNode = currentNode.next;
                lastNode = lastNode.next;
            }

            currentNode.next = null;
            lastNode.next = head;
            k--;
        }

        return head;
    }
}
